<?php
include($_SERVER['DOCUMENT_ROOT'] . '/Biohof/inc/application_settings.php');


$target = (isset($_REQUEST['T'])) ? $_REQUEST['T'] : null;
$output = null;

switch ($target) {
	case 'GALLERY_PREVIEW' :
		$output = loadGalleryPreview();
		break;
	case 'GALLERY':
		$output = loadGallery();
		break;
	default :
		$output = 'No data found';
		break;
}

echo $output;


/**
 * loads all images with their information of the gallery
 * @return		string		$output;
 */
function loadGallery(){
	$galleryId = (isset($_REQUEST['GALLERY_ID'])) ? $_REQUEST['GALLERY_ID'] : null;
	$output = '';
	
	if (is_numeric($galleryId)){
		$sqlObj = Sql_Object::getInstance();
		$sqlObj->connect();
		
		$langCode = (LANGUAGE == ENGLISH) ? 'en' : 'de';
		$name = 'name_' . $langCode;
		$sql = 'select height, width, file_id, ' . $name . ' from ' . DB_BIO . '.gallery_image_view where gallery_id = ? and active = ?';
		$params = array($galleryId, ACTIVE);
		
		$sqlObj->execute($sql, $params);
		while(($row = $sqlObj->fetch()) != null)
			$output .= getImageOutput($row[$name], $row['file_id'], $row['height'], $row['width']);
		
		
		$sqlObj->disconnect();
	}
	return $output;	
}


/**
 * returns the html output for images in gallery
 * @param 		string		 $name
 * @param 		int			 $fileId
 */
function getImageOutput($name, $fileId, $height, $width){
	$output .= '<div class="galleryImage" style="height: ' . ($height + 20)  . 'px; width: ' . $width . 'px">';
	$output .= 	'<div style="height: ' . $height  . 'px; width: ' . $width . 'px">';
	$output .= 		'<img style="display: none" src="' . WEBSITE_DIR . '/gallery/thumbnail.php?T=IMAGE&HEIGHT=' . $height . '&WIDTH=' . $width . '&FILE_ID=' . $fileId . '" width="' . $width. '" height="' . $height . '"/>';
	$output .=	'</div>';
	$output .= '<div class="galleryImageName">' . $name . '</div>';
	$output .= '</div>';
	return $output;
}

/**
 * loads a preview of all existing galleries
 * @return		string		$output;
 */
function loadGalleryPreview() {
	$output = '';
	$langCode = (LANGUAGE == ENGLISH) ? 'en' : 'de';
	
	$sqlObj = Sql_Object::getInstance();
	$sqlObj->connect();
	
	$name = 'name_' . $langCode;
	$sql = 'select gallery_id, ' . $name .  ' from ' . DB_BIO . '.gallery_view gv where active = ? ';
	$sql .= 'and gv.gallery_id in (select gi.gallery_id from ' . DB_BIO . '.gallery_image gi where gi.active = ?) order by ' . $name;
	$params = array(ACTIVE, ACTIVE);
	
	$sqlObj->execute($sql, $params);
	while(($row = $sqlObj->fetch()) != null){
		$output .= getGalleryPreview($row['gallery_id'], $row[$name]);
	}
	
	$sqlObj->disconnect();
	return $output;
}


/**
 * forms the preview output
 * @param 		int			$galleryId
 * @param 		string		$name
 * @return		string		$output
 */
function getGalleryPreview($galleryId, $name){
	$output = '<div class="galleryPreview" onclick="loadGallery(' . $galleryId . ');">';
	$output .= 	'<div style="height:170px; width:190px">';
	$output .= 		'<img style="display: none" src="' . WEBSITE_DIR . '/gallery/thumbnail.php?T=GALLERY_PREVIEW&GALLERY_ID=' . $galleryId . '" width="190" height="170" />';
	$output .=	'</div>';
	$output .= '<div class="galleryName">' . $name . '</div>';
	$output .= '</div>';
	//$output .= $name;
	return $output;
}
?>